<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Agp\CoreBundle\Manager;

use Doctrine\ORM\EntityManager;
use Agp\CoreBundle\Entity\ListaPreco;
use Agp\CoreBundle\Manager\BaseManager;
/**
 * Description of ListaManager
 *
 * @author bondcs
 */
class ListaManager extends BaseManager
{
    public function changeSituacao($entity){
        
        if ($entity->isAtivo()){
            $entity->setSituacao(ListaPreco::SIT_INATIVO);
        }else{
            $entity->setSituacao(ListaPreco::SIT_ATIVO);
        }
        
        $this->update($entity);
        
    }
    
    public function updateWithKey($entity, $keys) {
        parent::update($entity);
        $manager = $this->container->get('agp.lista.manager');

        foreach ($keys as $i => $k){
            if ($i > 0){
                $lista = $manager->findById($k);
                $lista->getSetores()->clear();
                $lista->setTipoSetor($entity->getTipoSetor());
                $lista->setVlrSetorFloat($entity->getVlrSetor());
                $lista->setAmbienteForGrupo($entity->getAmbienteForGrupo());
                foreach ($entity->getSetores() as $s){
                    $lista->addSetor(clone $s);
                    $this->persist($s);
                }
                
                $lista->setQtdeSetorFilho($lista->getSetores()->count());
                parent::updateNoFlush($lista);
            }
            
        }
        
        $this->em->flush();
    }
    
    public function changeSituacaoSetor($entity){
        
        if ($entity->getSituacaoSetor() == ListaPreco::SIT_SETOR_DISPONIVEL){
            $entity->setSituacaoSetor(ListaPreco::SIT_SETOR_INDISPONIVEL);
        }else{
            $entity->setSituacaoSetor(ListaPreco::SIT_SETOR_DISPONIVEL);
        }
        
        $this->update($entity);
        
    }
    
}

?>
